package com.example.forum.services.impl;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.dao.BoardMapper;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Board;
import com.example.forum.services.IBoardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: wenjingyuan
 * @Date: 2023/09/24/19:54
 * @Description:
 */
@Slf4j
@Service
public class BoardServiceImpl implements IBoardService {

    @Resource
    private BoardMapper boardMapper;
    @Override
    public List<Board> selectByNum(Integer num) {
        //校验参数
        if(num<=0){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE .toString());
            //抛出异常，统一抛出ApplicationException
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //调用DAO 来查询数据库
        List<Board> boards = boardMapper.selectByNum(num);
        return boards;
    }

    @Override
    public void addOneArticleCountById(Long id) {
        //校验参数
        if(id==null ||id<=0){
            //打印日志
            log.info(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }

        //参数没问题
        //调用DAO方法,查询板块内容
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board==null){
            //打印日志
            log.info(ResultCode.ERROR_IS_NULL.toString()+"board id="+id);
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        //更新帖子数量
        Board updateBoard=new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount()+1);
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if(row!=1){
            //打印日志
            log.warn(ResultCode.FAILED.toString());
            //抛出异常
            throw  new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public void subOneArticleCountById(Long id) {
        //校验参数
        if(id==null ||id<=0){
            //打印日志
            log.info(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }

        //参数没问题
        //调用DAO方法,查询板块内容
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board==null){
            //打印日志
            log.info(ResultCode.ERROR_IS_NULL.toString()+"board id="+id);
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        //更新帖子数量
        Board updateBoard=new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount()-1);
        if(updateBoard.getArticleCount()<0){
            updateBoard.setArticleCount(0);
        }
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if(row!=1){
            //打印日志
            log.warn(ResultCode.FAILED.toString());
            //抛出异常
            throw  new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public Board selectById(Long id) {
        //校验参数
        if(id==null ||id<=0){
            //打印日志
            log.info(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        Board board = boardMapper.selectByPrimaryKey(id);
        return board;
    }
}
